package 自己写一个json解析器测试;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CharReader {

    private final char[] buffer;

    private int position = 0;

    private int size;

    public CharReader(String strJson) {
        StringBuilder stringBuilder = new StringBuilder();
        Pattern compile = Pattern.compile("(\\S)*");
        Matcher matcher = compile.matcher(strJson);
        while (matcher.find()) {
            stringBuilder.append(matcher.group());
        }

        this.buffer = stringBuilder.toString().toCharArray();

        this.size = this.buffer.length;

    }


    public char current() {
        if (position - 1 >= size) {
            return (char) -1; // 结束
        }
        int max = Math.max(0, position - 1);
        return buffer[max];
    }


    public char next() {
        if (!hasMore()) {
            return (char) -1;
        }
        int i = position++;
        return buffer[i];
    }

    public void back() {
        position = Math.max(0, --position);
    }

    public boolean hasMore(){
        return position < size;
    }
}
